package com.spic.business.xfalarm.controller;

import com.spic.business.alarm.utils.DateTimeUtil;
import com.spic.business.xfalarm.DPage;
import com.spic.business.xfalarm.domain.vo.AlarmInfoDateVo;
import com.spic.business.xfalarm.mapper.OdsXfMapper;
import com.spic.business.xfalarm.service.IAlarmInfoDateService;
import com.spic.business.xfalarm.service.IDeviceInfoService;
import com.spic.common.core.constant.HttpStatus;
import com.spic.common.core.utils.ServletUtils;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 290512
 * @create 2022-05-25 14:46
 */
@RestController
@RequestMapping("/xfAlarm")
public class XfAlarmController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(XfAlarmController.class);

    @Autowired
    private IAlarmInfoDateService alarmInfoDateService;
    @Autowired
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private OdsXfMapper odsXfMapper;

    @GetMapping("/jobXfAlarm")
    public AjaxResult jobXfAlarm(){
        initData();
        return AjaxResult.success("成功");
    }

    @GetMapping("/list")
    public TableDataInfo getList(AlarmInfoDateVo alarmInfoDateVo){
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");
        List<Map<String, Object>> list = odsXfMapper.selectAllList(alarmInfoDateVo);
        DPage<Map<String, Object>> page = new DPage<>(list,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;

    }

    @GetMapping("/device/list")
    public TableDataInfo getDeviceList(AlarmInfoDateVo alarmInfoDateVo){
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");
        List<Map<String, Object>> list = odsXfMapper.selectDeviceAll(alarmInfoDateVo);
        DPage<Map<String, Object>> page = new DPage<>(list,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @PutMapping("/updateDeviceInfo")
    public AjaxResult updateDeviceInfo(@RequestBody Map map) {
        if(map.get("cdiDeviceId") == null) {
            return AjaxResult.error("请求内容有误");
        }
        return toAjax(odsXfMapper.updateDeviceInfo(map));
    }

    @GetMapping("/device/{id}")
    public AjaxResult selectDeviceInfoById(@PathVariable("id") String id) {
        return AjaxResult.success(odsXfMapper.selectDeviceInfoById(id));
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(odsXfMapper.selectAlarmInfoDateById(id));
    }

    public void initData() {
        log.info("消防告警数据同步至ods开始******************************");
        boolean monthBeginDate = DateTimeUtil.isMonthBeginDate();
        if(monthBeginDate){
            String lastDate = DateTimeUtil.getLastDate(new Date());
            alarmInfoDateService.alarmInfoQueryList(lastDate);
        }
        String yyyyMMdd = DateTimeUtil.dateToStr(new Date(), "yyyyMM");
        alarmInfoDateService.alarmInfoQueryList(yyyyMMdd);
        deviceInfoService.queryDeviceInfoList();
        log.info("消防告警数据同步至ods结束******************************");
    }
}
